<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Papa
 *
 * @author dragon.hu @2016.3.10
 */
require_once 'BaseChannel.php';

//啪啪游戏厅
class Papa implements BaseChannel 
{
	static $app_key = "16000095";
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="8b923639828ecbf3ff12bfe60ad93edfaa29c95d2b269276653afc99c545c596"; //secretkey
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //秘钥
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("papa", $_REQUEST);
		
		$app_order_id = $_REQUEST['app_order_id'];  //游戏内部订单号
		$money_amount = $_REQUEST['money_amount'];  //金额(元)
		$pa_open_order_id = $_REQUEST['pa_open_order_id'];  //啪啪订单号
		$sign = $_REQUEST['sign'];  //签名
		
		$arr = $_REQUEST;
		ksort($arr);
		$mystr = "";
		foreach ($arr as $k=>$v)
		{
			if ($k == 'sign')  continue;
			$mystr .= $k.'='.$v.'&';
		}
		$mystr = substr($mystr, 0, -1);
		$mystr = self::$app_key.$paymentKey.$mystr;
		$mysign = md5($mystr);
		
		if($sign == $mysign)  
		{ //验证通过
			 
			//进行发货
			if(false == $orderLog->recordOrder(STATUS_SUCCESS, $money_amount, $app_order_id, "papa", $platform,$pa_open_order_id))
			{
				$ret="failure";
				$orderLog->log("papa",array("order"=>$app_order_id,"money"=>$money_amount,"serverresult"=>"faild"));
			}else 
			{
				//支付成功
				$ret="ok";
				$orderLog->log("papa",array("order"=>$pa_open_order_id,"money"=>$money_amount,"serverresult"=>"success"));
			}
			 
		} else {
			//"$sig验证失败"
			$ret="failure";
			$orderLog->log("papa","  order:$pa_open_order_id  sign error sign:$sign mysign:$mysign");
		}
		return $ret;
	}
	
}